using System;
using System.IO;
using System.Runtime.CompilerServices;
namespace Chadsoft.CTools.Models
{
   

    public class Matrix3x1
    {
        public Matrix3x1()
        {
        }
        int parselittle(string input)
        {
            int test = 0;
            test = int.Parse(input.ToCharArray()[6].ToString() + input.ToCharArray()[7].ToString() + input.ToCharArray()[4].ToString() + input.ToCharArray()[5].ToString() + input.ToCharArray()[2].ToString() + input.ToCharArray()[3].ToString() + input.ToCharArray()[0].ToString() + input.ToCharArray()[1].ToString(), System.Globalization.NumberStyles.HexNumber);
            return test;
        }
        public Matrix3x1(EndianBinaryReader reader)
        {
            if ((reader.BaseStream.Length - reader.BaseStream.Position) < 12L)
            {
                throw new InvalidDataException();
            }
            this.X = parselittle(BitConverter.ToString(reader.ReadBytes(4)).Replace("-", ""));
            this.Y = parselittle(BitConverter.ToString(reader.ReadBytes(4)).Replace("-", ""));
            this.Z = parselittle(BitConverter.ToString(reader.ReadBytes(4)).Replace("-", ""));
        }

        public Matrix3x1(float a)
        {
            this.X = a;
            this.Y = a;
            this.Z = a;
        }

        public Matrix3x1(float x, float y, float z)
        {
            this.X = x;
            this.Y = y;
            this.Z = z;
        }

        public Matrix3x1 Cross(Matrix3x1 right)
        {
            return new Matrix3x1((this.Y * right.Z) - (this.Z * right.Y), (this.Z * right.X) - (this.X * right.Z), (this.X * right.Y) - (this.Y * right.X));
        }

        public float Dot(Matrix3x1 n)
        {
            return (((this.X * n.X) + (this.Y * n.Y)) + (this.Z * n.Z));
        }

        public bool Equals(Matrix3x1 obj)
        {
            return (((obj.X == this.X) && (obj.Y == this.Y)) && (obj.Z == this.Z));
        }

        public override bool Equals(object obj)
        {
            return (((obj != null) && !(base.GetType() != obj.GetType())) && this.Equals((Matrix3x1) obj));
        }

        public bool Equals(Matrix3x1 obj, float error)
        {
            return (((Math.Abs((float) (obj.X - this.X)) < error) && (Math.Abs((float) (obj.Y - this.Y)) < error)) && (Math.Abs((float) (obj.Z - this.Z)) < error));
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public float Length()
        {
            return (float) Math.Sqrt((double) (((this.X * this.X) + (this.Y * this.Y)) + (this.Z * this.Z)));
        }

        public void MinMax(Matrix3x1 min, Matrix3x1 max)
        {
            if (min.X > this.X)
            {
                min.X = this.X;
            }
            if (min.Y > this.Y)
            {
                min.Y = this.Y;
            }
            if (min.Z > this.Z)
            {
                min.Z = this.Z;
            }
            if (max.X < this.X)
            {
                max.X = this.X;
            }
            if (max.Y < this.Y)
            {
                max.Y = this.Y;
            }
            if (max.Z < this.Z)
            {
                max.Z = this.Z;
            }
        }

        public Matrix3x1 Normalise()
        {
            float num = this.Length();
            return new Matrix3x1(this.X / num, this.Y / num, this.Z / num);
        }

        public static Matrix3x1 operator +(Matrix3x1 left, Matrix3x1 right)
        {
            return new Matrix3x1(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
        }

        public static Matrix3x1 operator /(Matrix3x1 left, float right)
        {
            return new Matrix3x1(left.X / right, left.Y / right, left.Z / right);
        }

        public static Matrix3x1 operator *(Matrix3x1 left, float right)
        {
            return new Matrix3x1(left.X * right, left.Y * right, left.Z * right);
        }

        public static Matrix3x1 operator -(Matrix3x1 left, Matrix3x1 right)
        {
            return new Matrix3x1(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
        }

        public static Matrix3x1 operator -(Matrix3x1 value)
        {
            return new Matrix3x1(-value.X, -value.Y, -value.Z);
        }

        public void Set(Matrix3x1 copy)
        {
            this.X = copy.X;
            this.Y = copy.Y;
            this.Z = copy.Z;
        }

        public void Write(EndianBinaryWriter writer)
        {
            writer.Write(Int32ToBytes((int)this.X), 0, 4);
            writer.Write(Int32ToBytes((int)this.Y), 0, 4);
            writer.Write(Int32ToBytes((int)this.Z), 0, 4);
        }
        private static byte[] Int32ToBytes(Int32 int32Input)
        {
            byte[] numBytesBuf = new byte[4];
            return System.BitConverter.GetBytes(int32Input);
        }

        public float X { get; set; }

        public float Y { get; set; }

        public float Z { get; set; }
    }
}

